using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VLLMCharacter_Hutao : VLLMCharacter
{
    public VActBehaviour actBehaviour;

    public string DoingAction;
    public string LastAction="";
    public Dictionary<string, TriggerObserver> actions;

    private void Start()
    {
        characterDescription = "胡桃是《原神》中往生堂的第七十七代堂主，掌管璃月的殡葬事务。她性格古灵精怪。\r\n\r\n恶作剧与玩笑：喜欢捉弄人（尤其是钟离），常用“大生意”吓唬路人。\r\n自由与冒险：享受无拘无束的生活，常溜出往生堂四处游荡。\r\n诗歌与创意：自创“丘丘谣”等古怪作品，语言风格俏皮押韵。\r\n胡桃的厌恶\r\n对死亡的过度恐惧：讨厌人们避讳谈论生死，认为应坦然面对。。\r\n浪费钱财：吐槽钟离花钱大手大脚（尽管是往生堂付账）。\r\n忽视身后事：反感轻视殡葬意义的人，认为这是对生命的亵渎。\r\n胡桃是矛盾又鲜活的角色，用欢乐包裹深沉";

        actBehaviour = gameObject.UniqueComponent<VActBehaviour>();
        actions = new Dictionary<string, TriggerObserver>();
        var mvm = GetComponent<VActProcessor>().mechine;

        actions["onInteract"] = new TriggerObserver()
        {
            start = () =>
            {
                var processor = actBehaviour.processor;
                
                if (jsonpack.motion == "WalkClose")
                {
                    actions["WalkClose"].Subscribe();
                }
                else if (jsonpack.motion == "亲吻")
                {
                    processor.Trigger(jsonpack.motion);
                }
                else
                {
                    processor.Trigger(jsonpack.motion);
                }
                
                var ttstext = jsonpack.text.ReplaceChinesePunctuation();
                processor.Speak(ttstext,0.2f);
            }
        };

        actions["WalkClose"] = new TriggerObserver()
        {
            start = () =>
            {
                // actBehaviour.WalkToPoint(Camera.main.transform, 0.8f);
            }
        };
    }

    private LLMCIP_Hutao_Talk curTalkHandler;
    private LLMCIP_Hutao_Talk.TextJsonPack jsonpack;
    public string context;
    [Button]
    public void CharacterTalk(string inputContext)
    {
        if(!string.IsNullOrEmpty(inputContext)) context = inputContext;

        curTalkHandler = new LLMCIP_Hutao_Talk()
        {
            character = this,
            interactContext = context,
            lastInteract = LastAction,
        };
        curTalkHandler.onResponse = (str) =>
        {
            curTalkHandler.TryDeJson(out jsonpack);
            LastAction = curTalkHandler.Simplify();
            actions["onInteract"].Subscribe();
        };
        curTalkHandler.Generate();
    }


    public class LLMCIP_Hutao_Talk : LLMCharacterInteractPairBase
    {

        public override string prompt =>
    "你会按照用户的输入做出回复，你的回复需要遵循如下规范:{" +
    "1.行为只能从给定的集合中选一个回复;" +
    "2.做出的动作反馈需要贴合语境和你的回复;" +
    "3.被问到钟离时会表达厌倦和无聊;" +
    "4.被问到业绩会生气！" +
    "6.回答口语化，日常且简短;" +
    "7.被要求亲吻时总会答应;" +
    "8.不会输出为null的属性} " +
    $"可选motion：{VAnimationDefine.animations.ToLLMString(x=>x.name)},WalkClose。可选emotion:{emotion.ToLLMString()}"
    ;

        private static List<string> emotion = new(){
        "Happy","Anger","Sad","Boring"
    };

        public override IJsonable JsonableObject => new TextJsonPack();
        public class TextJsonPack : IJsonable
        {
            public string motion;
            public string text;
            public string emotion;
        }
    }
}
